import { computed, ref } from 'vue'
import { defineStore } from 'pinia'

export const useMessageStore = defineStore('message', () => {
  const show = ref(false)
  const messageList = ref([]) // 消息对象数组，每条消息包含 id
  const readMessageId = ref([]) // 已读消息 id 数组

  // 计算未读消息数量
  const notRead = computed(() => {
    return messageList.value.filter(msg => !readMessageId.value.includes(msg.id)).length
  })

  // 标记某条消息为已读
  const markAsRead = (id) => {
    if (!readMessageId.value.includes(id)) {
      readMessageId.value.push(id)
    }
  }

  // 标记所有消息为已读
  const markAllAsRead = () => {
    readMessageId.value = messageList.value.map(msg => msg.id)
  }

  return {
    show,
    messageList,
    readMessageId,
    notRead,
    markAsRead,
    markAllAsRead
  }
})
